package com.zut.edu.utils;

import android.Manifest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.DrawableRes;

import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class CameraUtils {

    private static Uri photoUri;
    public static final int NONE = 0;
    public static final int PHOTOHRAPH = 1;// 拍照
    public static final int PHOTOZOOM = 2; // 相册
    public static final int PHOTORESULT = 3;// 结果
    public static final String IMAGE_UNSPECIFIED = "image/*";
    /**
     * 进入照相机
     * @param activity
     */
    public static void startCamera(Activity activity) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE"
        ContentValues values = new ContentValues();
        photoUri = activity.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);
        /**-----------------*/
        activity.startActivityForResult(intent, PHOTOHRAPH);
    }

    /**
     * 进入相册
     * @param activity
     */
    public static  void startPhotoAlbum(Activity activity) {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
        activity.startActivityForResult(intent, PHOTOZOOM);
    }

    /**
     * 裁剪照片 跳系统的activity
     * @param activity
     * @param uri
     */
    public static void startPhotoZoom(Activity activity, Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 300); // 决定图片的像素
        intent.putExtra("outputY", 300); // 决定图片的像素
        intent.putExtra("return-data", true);
        activity.startActivityForResult(intent, PHOTORESULT);
    }


    /**
     * 相册||照相 结果返回处理 放在Activity的onActivityResult里
     * @param activity
     * @param requestCode
     * @param resultCode
     * @param data
     * @return
     */
    public static Object[] handleResultFromCameraOrPhotos(Activity activity , int requestCode, int resultCode, Intent data) {
        Log.e("xxx",requestCode+""+resultCode);
        if (resultCode == NONE)
            return null;
        // 拍照
        if (requestCode == PHOTOHRAPH) {
            Context applicationContext = activity.getApplicationContext();
            String name="temp";//+applicationContext.setNum();
            name="temp";
            String path = Environment.getExternalStorageDirectory()+"/"+name+".jpg";
            System.out.println(path);
            File picture = new File(path);
            photoUri = data.getData();
            startPhotoZoom(activity,photoUri);
        }

        if (data == null)
            return null;

        // 读取相册缩放图片
        if (requestCode == PHOTOZOOM) {
            photoUri = data.getData();
            startPhotoZoom(activity,data.getData());
        }
        // 处理结果
        if (requestCode == PHOTORESULT) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                //photoUri = data.getData();
                String picPath = null;
                String[] pojo = {MediaStore.Images.Media.DATA};
                Cursor cursor = activity.managedQuery(photoUri, pojo, null, null,null);
                if(cursor != null )
                {

                    int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
                    cursor.moveToFirst();
                    picPath = cursor.getString(columnIndex);
                    //cursor.close();
                }

                Log.d("TAG", picPath +"");
                Bitmap photo = extras.getParcelable("data");
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);// (0 - 100)压缩文件
                Object[] hashMap = new Object[2];
                hashMap[1] = photo;
                hashMap[0] = picPath;
                //photo.recycle();
                return hashMap;
            }
        }
        return null;

    }

    public static void initRadiusImageView(QMUIRadiusImageView imageView, @DrawableRes int resId){//剪切圆形头像函数
        imageView.setImageResource(resId);
        imageView.setCircle(true);
        imageView.setCornerRadius(100);
        imageView.setOval(false);//是否椭圆
        imageView.setBorderWidth(1);//设置边缘宽度
    }
}
